/*
   Copyright 2009 Bloo. All Rights Reserved.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package org.youtensils.www.client.widgets.dialogs;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * A dialog box alerting the user to an error.
 * @author Bobby Soares
 */
public class ErrorDialog extends Dialog {
	private VerticalPanel contentPane;
	private HorizontalPanel buttonPane;
	private HTML errorLabel;
	private Runnable continueCallback;
	
	/**
	 *  A constructor.
	 */
	public ErrorDialog() {
		super("Error", true);
		this.contentPane = new VerticalPanel();
		this.contentPane.setWidth("500px");
		this.buttonPane = new HorizontalPanel();
		this.errorLabel = new HTML();
		this.errorLabel.setStylePrimaryName("gwt-DialogBox-Error-Detail");
		this.contentPane.add(new HTML("An unexpected error occurred. See below for details:"));
		this.contentPane.add(this.errorLabel);
		Button ok = new Button("OK");
		ok.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				ErrorDialog.this.hide();
				if (ErrorDialog.this.continueCallback != null) {
					ErrorDialog.this.continueCallback.run();
				}
			}
		});
		Button submit = new Button("Submit error to the issue tracker");
		submit.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				ErrorDialog.this.hide();
				Window.open("http://code.google.com/p/youtensils/issues/entry", "SubmitError", "");
				if (ErrorDialog.this.continueCallback != null) {
					ErrorDialog.this.continueCallback.run();
				}
			}
		});
		closeButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				ErrorDialog.this.hide();
			}
		});
		this.buttonPane.add(ok);
		this.buttonPane.add(submit);
		this.buttonPane.setStylePrimaryName("gwt-Dialog-ButtonPane");
		this.contentPane.add(buttonPane);
		this.setContentWidget(this.contentPane);
	}
	
	/**
	 * Updates the error message and displays the error dialog.
	 * 
	 * @param error The error message to display.
	 * @param continueCallback The callback to be invoked when the user closes the dialog window.
	 */
	public void center(String error, Runnable continueCallback) {
		this.continueCallback = continueCallback;
		this.errorLabel.setHTML("<br />" + error + "<br />");
		super.center();
	}
}

